package cn.swenty.backstage.framework.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataSourceContextHolder {
		private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceContextHolder.class);
	    public static final String DATA_SOURCE_MASTER = "MASTER";
	    public static final String DATA_SOURCE_SLAVE = "SLAVE";
	    // 线程本地环境
	    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

	    // 设置数据源类型
	    public static void setType(String type) {
	    	if(type == null ||type.trim().equals("")){
	    		type = DATA_SOURCE_MASTER;
	    	}
	    	contextHolder.set(type);
	        if(LOGGER.isDebugEnabled())
	            LOGGER.debug("==============切换数据源，类型："+type+"================");
	        
	    }
	    // 获取数据源类型
	    public static String getType() {
	        return (contextHolder.get());
	    }
	    // 清除数据源类型
	    public static void clearType() {
	        contextHolder.remove();
	    }
}
